//
//  ServiceConfig.m
//  YLNetworkingFrameworks
//
//  Created by Topsun on 16/6/2.
//  Copyright © 2016年 tcl. All rights reserved.
//

#import "ServiceConfig.h"

#define APIKEY          @"API"
#define CONFIGKEY       @"Config"
#define NECESSARYKEY    @"Necessary"

#define API_URL                 @"apiurl"
#define API_METHOD              @"requestMethod"
#define API_METHODINTERFACE     @"requestInterface"

#ifdef DEBUG
#define APISERVERURLKEY @"apiserverurl_debug"
#define APPKEYVALUEKEY  @"appkey_debug"
#else
#define APISERVERURLKEY @"apiserverurl_release"
#define APPKEYVALUEKEY  @"appkey_release"
#endif

// 测试用 -----
#define plistFile @"serviceConfig.plist"
#define fileType ePlistFilePath

static ServiceConfig * static_serviceConfig;
static NSDictionary * static_configDic;

@interface ServiceConfig ()

// 获得APP的配置参数字典
- (NSDictionary *) getConfigDictionary;

// 获得所有的API接口字典
- (NSDictionary *) getAPIDictionary;

@end

@implementation ServiceConfig

// 初始化配置器
+ (void) initServicesConfig
{
    ServiceConfig *sc = [ServiceConfig defaultServiceConfig];
    [sc initWithData:plistFile ofType:fileType];
    
}

+ (ServiceConfig *)defaultServiceConfig
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        if (nil == static_serviceConfig) {
            static_serviceConfig = [[ServiceConfig alloc]init];
        }
    });
    return static_serviceConfig;
}

- (void) initWithData:(NSString *)data ofType:(XDataType)dataType
{
    NSDictionary * dicObj = nil;
    NSError * error;
    
    switch (dataType) {
        case ePlistFilePath:
        {
            NSString * filePath = [[[NSBundle mainBundle]resourcePath]stringByAppendingPathComponent:data];
            dicObj = [[NSDictionary alloc]initWithContentsOfFile:filePath];
        }
            break;
        case eJsonFilePath:
        {
            NSString * filePath = [[[NSBundle mainBundle]resourcePath]stringByAppendingPathComponent:data];
            NSString * jsontext = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
            dicObj = [NSJSONSerialization JSONObjectWithData:[jsontext dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableContainers error:&error];
        }
            break;
        case eJson:
        {
            dicObj = [NSJSONSerialization JSONObjectWithData:[data dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableContainers error:&error];
        }
            break;
        case ePlist:
        {}
            break;
            
        default:
            break;
    }
    
    if (dicObj != nil && [self isCorrect:dicObj]) {
        static_configDic = dicObj;
    }else{
        NSLog(@"config file failed.");
    }
}

// 判断字典是否是需要的字典结构
- (BOOL) isCorrect:(NSDictionary*) dicObj
{
    if ( ![dicObj isKindOfClass:[NSDictionary class]] ) {
        return NO;
    }
    
    id apiobj = [dicObj objectForKey:APIKEY];
    id configobj = [dicObj objectForKey:CONFIGKEY];
    
    return apiobj != nil && configobj != nil;
    
}
/**
 *  @author 李悦麟, 16-06-02
 *
 *  获得serviceConfig.plist文件中Config的配置参数字典
 *
 *  @return serviceConfig.plist文件中Config的配置参数字典
 *
 *  @since 1.0
 */
- (NSDictionary *) getConfigDictionary
{
    return [static_configDic objectForKey:CONFIGKEY];
}

/**
 *  @author 李悦麟, 16-06-02
 *
 *  获得serviceConfig.plist文件中API的配置参数字典
 *
 *  @return serviceConfig.plist文件中API的配置参数字典
 *
 *  @since 1.0
 */
- (NSDictionary *) getAPIDictionary
{
    return [static_configDic objectForKey:APIKEY];
}


/**
 *  @author 李悦麟, 16-06-02
 *
 *  获取后台服务器的host
 *
 *  @return 后台服务器的host
 *
 *  @since 1.0
 */
- (NSString *) getServiceHostURLString
{
    NSDictionary * configDic = [self getConfigDictionary];
    NSString * hostStr = [configDic objectForKey:APISERVERURLKEY];
    return hostStr;
}

/**
 *  @author 李悦麟, 16-06-02
 *
 *  获得一个接口的参数表
 *
 *  @param interfaceKey 接口名
 *
 *  @return 接口的所有配置信息
 *
 *  @since 1.0
 */
- (NSDictionary *)getAPIConfigForInterfacekey:(NSString*)interfaceKey
{
    NSDictionary *apiDic = [[self getAPIDictionary] objectForKey:interfaceKey];
    
    if ( [apiDic isKindOfClass:[NSDictionary class]] ) {
        
        return apiDic;
    }
    
    return nil;
}

/**
 *  @author 李悦麟, 16-06-02
 *
 *  获得一个接口的后缀URL地址
 *
 *  @param interfaceKey 接口名
 *
 *  @return 接口的后缀URL地址
 *
 *  @since 1.0
 */
- (NSString *)getAPIURLForInterfacekey:(NSString*)interfaceKey
{
    NSDictionary *apiDic = [[self getAPIDictionary] objectForKey:interfaceKey];
    
    if ( [apiDic isKindOfClass:[NSDictionary class]] ) {
        
        NSString *urlstring = [apiDic objectForKey:API_URL];
        
        if ( [urlstring isKindOfClass:[NSString class]] ) {
            return urlstring;
        }
    }
    
    return @"";
}

/**
 *  @author 李悦麟, 16-06-02
 *
 *  获得一个接口的http请求方式
 *
 *  @param interfaceKey 接口名
 *
 *  @return http请求方式 (GET || POST || ...)
 *
 *  @since 1.0
 */
- (NSString*) getRequestMethodForInterfacekey:(NSString*)interfaceKey
{
    NSDictionary *apiDic = [[self getAPIDictionary] objectForKey:interfaceKey];
    
    if ( [apiDic isKindOfClass:[NSDictionary class]] ) {
        
        NSString *methodstring = [apiDic objectForKey:API_METHOD];
        
        if ( [methodstring isKindOfClass:[NSString class]] ) {
            return methodstring;
        }
    }
    
    return @"";
}

/**
 *  @author 李悦麟, 16-06-02
 *
 *  获得一个接口的代号
 *
 *  @param interfaceKey 接口名
 *
 *  @return 接口代号
 *
 *  @since 1.0
 */
- (NSString*) getRequestInterfaceForInterfacekey:(NSString*)interfaceKey
{
    NSDictionary *apiDic = [[self getAPIDictionary] objectForKey:interfaceKey];
    
    if ( [apiDic isKindOfClass:[NSDictionary class]] ) {
        
        NSString *methodstring = [apiDic objectForKey:API_METHODINTERFACE];
        
        if ( [methodstring isKindOfClass:[NSString class]] ) {
            return methodstring;
        }
    }
    
    return @"";
}

@end
